package com.byt.common.view.finder.addresswheelfinder.picker;

import android.app.Activity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.byt.common.view.finder.addresswheelfinder.widget.WheelView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;

import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static com.byt.common.view.finder.addresswheelfinder.popup.BasicPopup.MATCH_PARENT;

/**
 * 双项选择器，选择两项，数据不联动。
 */
public class ThreePicker extends WheelPicker {
    private List<String> firstData = new ArrayList<>();
    private List<String> secondData = new ArrayList<>();
    private List<String> threeData = new ArrayList<>();
    private int selectedFirstIndex = 0;
    private int selectedSecondIndex = 0;
    private int selectedThreeIndex = 0;
    private OnWheelListener onWheelListener;
    private OnPickListener onPickListener;
    private CharSequence periodPrefixLabel, cycleSuffixLabel, threeLabel;

    public ThreePicker(Activity activity, List<String> firstData, List<String> secondData, List<String> threeData) {
        super(activity);
        this.firstData = firstData;
        this.secondData = secondData;
        this.threeData = threeData;
    }

    /**
     * 设置选择的索引
     *
     * @param firstIndex
     * @param secondIndex
     */
    public void setSelectedIndex(int firstIndex, int secondIndex, int threeIndex) {
        if (firstIndex >= 0 && firstIndex < firstData.size()) {
            selectedFirstIndex = firstIndex;
        }
        if (secondIndex >= 0 && secondIndex < secondData.size()) {
            selectedSecondIndex = secondIndex;
        }
        if (threeIndex >= 0 && threeIndex < threeData.size()) {
            selectedThreeIndex = threeIndex;
        }
    }

    /**
     * 设置顶部的文本
     *
     * @param secondPrefixLabel
     * @param secondSuffixLabel
     */
    public void setTopLabel(CharSequence secondPrefixLabel, CharSequence secondSuffixLabel, CharSequence threeLabel) {
        this.periodPrefixLabel = secondPrefixLabel;
        this.cycleSuffixLabel = secondSuffixLabel;
        this.threeLabel = threeLabel;
    }

    /**
     * 获取选择的第一滚动视图条目
     *
     * @return
     */
    public String getSelectedFirstItem() {
        if (firstData.size() > selectedFirstIndex) {
            return firstData.get(selectedFirstIndex);
        }
        return "";
    }

    /**
     * 获取选择的第二滚动视图条目
     *
     * @return
     */
    public String getSelectedSecondItem() {
        if (secondData.size() > selectedSecondIndex) {
            return secondData.get(selectedSecondIndex);
        }
        return "";
    }

    /**
     * 获取选择的第二滚动视图条目
     *
     * @return
     */
    public String getSelectedThreeItem() {
        if (threeData.size() > selectedThreeIndex) {
            return threeData.get(selectedThreeIndex);
        }
        return "";
    }

    /**
     * 绘制视图内容
     *
     * @return
     */
    @NonNull
    @Override
    protected View makeCenterView() {
        //主题视图容器
        LinearLayout layout = new LinearLayout(activity);
        layout.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setGravity(Gravity.CENTER);

        LinearLayout periodLayout = createLinearLayoutLabelView();
        layout.addView(periodLayout);
        if (!TextUtils.isEmpty(periodPrefixLabel)) {
            TextView secondPrefixLabelView = createLabelView();
            secondPrefixLabelView.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
            secondPrefixLabelView.setText(periodPrefixLabel);
            periodLayout.addView(secondPrefixLabelView);
        }
        final WheelView periodView = createWheelView();
        periodView.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
        periodLayout.addView(periodView);

        LinearLayout cycleLayout = createLinearLayoutLabelView();
        layout.addView(cycleLayout);
        if (!TextUtils.isEmpty(cycleSuffixLabel)) {
            TextView cycleSuffixLabelView = createLabelView();
            cycleSuffixLabelView.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
            cycleSuffixLabelView.setText(cycleSuffixLabel);
            cycleLayout.addView(cycleSuffixLabelView);
        }
        final WheelView cycleView = createWheelView();
        cycleView.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
        cycleLayout.addView(cycleView);


        LinearLayout threeLayout = createLinearLayoutLabelView();
        layout.addView(threeLayout);
        if (!TextUtils.isEmpty(threeLabel)) {
            TextView threeSuffixLabelView = createLabelView();
            threeSuffixLabelView.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
            threeSuffixLabelView.setText(threeLabel);
            cycleLayout.addView(threeSuffixLabelView);
        }
        final WheelView threeView = createWheelView();
        threeView.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
        threeLayout.addView(threeView);

        periodView.setItems(firstData, selectedFirstIndex);
        periodView.setOnItemSelectListener(new WheelView.OnItemSelectListener() {
            @Override
            public void onSelected(int index) {
                selectedFirstIndex = index;
                if (onWheelListener != null) {
                    onWheelListener.onFirstWheeled(selectedFirstIndex, firstData.get(selectedFirstIndex));
                }
            }
        });
        cycleView.setItems(secondData, selectedSecondIndex);
        cycleView.setOnItemSelectListener(new WheelView.OnItemSelectListener() {
            @Override
            public void onSelected(int index) {
                selectedSecondIndex = index;
                if (onWheelListener != null) {
                    onWheelListener.onSecondWheeled(selectedSecondIndex, secondData.get(selectedSecondIndex));
                }
            }
        });

        cycleView.setItems(threeData, selectedThreeIndex);
        cycleView.setOnItemSelectListener(new WheelView.OnItemSelectListener() {
            @Override
            public void onSelected(int index) {
                selectedThreeIndex = index;
                if (onWheelListener != null) {
                    onWheelListener.onSecondWheeled(selectedThreeIndex, threeData.get(selectedThreeIndex));
                }
            }
        });
        return layout;
    }

    @Override
    public void onSubmit() {
        if (onPickListener != null) {
            onPickListener.onPicked(selectedFirstIndex, selectedSecondIndex, selectedThreeIndex);
        }
    }

    public void setOnWheelListener(OnWheelListener onWheelListener) {
        this.onWheelListener = onWheelListener;
    }

    public void setOnPickListener(OnPickListener onPickListener) {
        this.onPickListener = onPickListener;
    }

    /**
     * 数据条目滑动监听器
     */
    public interface OnWheelListener {

        void onFirstWheeled(int index, String item);

        void onSecondWheeled(int index, String item);

        void onThreeWheeled(int index, String item);

    }

    /**
     * 数据选择完成监听器
     */
    public interface OnPickListener {
        void onPicked(int selectedFirstIndex, int selectedSecondIndex, int selectedThreeIndex);
    }
}
